/**
 * 
 */
package ch14;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;

/**
 * @author sz
 *
 * create time：2012-5-18
 */
class Blockable extends Thread {
	  private Peeker peeker;
	  protected TextField state = new TextField(40);
	  protected int i;
	  public Blockable(Container c) {
	    c.add(state);
	    peeker = new Peeker(this, c);
	  }
	  public synchronized int read() { 
		  
		 // if(i >=5 ) {		  
		//	  i++;
		 // }
		
		  return i;
	  }
	  protected synchronized void update() {
	    state.setText(getClass().getName()
	      + " state: i = " + i);
	  }
	  public void stopPeeker() { 
	    // peeker.stop(); Deprecated in Java 1.2
	    peeker.terminate(); // The preferred approach
	  }
	}

	class Peeker extends Thread {
	  private Blockable b;
	  private int session;
	  private TextField status = new TextField(40);
	  private boolean stop = false;
	  public Peeker(Blockable b, Container c) {
	    c.add(status);
	    this.b = b;
	    start();
	  }
	  public void terminate() { stop = true; }
	  public void run() {
	    while (!stop) {
	      status.setText(b.getClass().getName()
	        + " Peeker " + (++session)
	        + "; value = " + b.read());
	      
	       try {
	        sleep(100);
	      } catch (InterruptedException e){}
	    }
	  }
	} ///:Continued